/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author thangvv0907i
 */
public class Login_DA {

    private Statement stmRow;
    private CallableStatement smt;
    private String user;
    private String pass;

    public Login_DA(String txtUserName, String txtPassword) {
        this.user = txtUserName;
        this.pass = txtPassword;
        try {
            stmRow = ConnectionDB.getConnection().createStatement();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }

    public int checkLogin() {
        int per = 0;
        try {
            smt = ConnectionDB.getConnection().prepareCall("{call proc_Login(?,?,?)}");
            smt.setString(1, user);
            smt.setString(2, pass);
            smt.registerOutParameter(3, Types.INTEGER);//Permission
            smt.execute();
            per = smt.getInt(3);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return per;
    }

    public Object[][] Select_DA() {
        String sqlStr = "select * from Users as u inner join Roles as r on u.R_ID = r.R_ID where u.UserName='" + user + "' and u.Password='" + pass + "'";
        String sqlCount = "Select count(*) from Users where UserName='" + user + "' and Password='" + pass + "'";
        ResultSet rs = null;
        ResultSet rsCount = null;
        ResultSetMetaData rsMeta = null;

        Object[][] objStd = null;
        try {
            rsCount = stmRow.executeQuery(sqlCount);
            rsCount.next();
            int row = rsCount.getInt(1);
            rs = stmRow.executeQuery(sqlStr);
            rsMeta = rs.getMetaData();
            int count = rsMeta.getColumnCount();
            objStd = new Object[row][count];
            int j=0;
            while (rs.next()) {
                for (int i = 0; i < count; i++) {
                    objStd[j][i] = rs.getString(i+1);
                }
                j++;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return objStd;

    }
}
